package com.bite.user.listener;

import com.bite.common.constant.Constant;
import com.bite.common.utils.JsonUtils;
import com.bite.common.utils.MailClient;
import com.bite.user.model.EmailUser;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Slf4j
public class UserQueueListener {

    @Autowired
    private MailClient mailClient;

    //    @RabbitListener(queues = Constant.USER_QUEUE_NAME)
//    public void listener(Message message, Channel channel) throws IOException {
//        long deliveryTag = message.getMessageProperties().getDeliveryTag();
//        try {
//            String body = new String(message.getBody());
//            log.info("[user.listener] 收到信息 body: {}", body);
//            // TODO 发送邮件
//
//            // 手动确认
//            channel.basicAck(deliveryTag, true); // 批量确认
//        } catch (Exception e) {
//            // 出错 重新确认
//            channel.basicNack(deliveryTag, true, true); // 批量确认 并且重新入队
//        }
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name = Constant.USER_QUEUE_NAME, durable = "true"),
                    exchange = @Exchange(
                        name = Constant.USER_EXCHANGE_NAME,
                        type = ExchangeTypes.FANOUT
                    )
            )
    )
    public void listener(Message message, Channel channel) throws IOException {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            String body = new String(message.getBody());
            log.info("[user.listener] 收到信息 body: {}", body);

            // 发送邮件
            EmailUser emailUser = JsonUtils.parseJson(body, EmailUser.class);
            mailClient.send(emailUser.getEmail(), "恭喜成功创建比特博客系统账号", creatContent(emailUser));
            // 手动确认
            channel.basicAck(deliveryTag, true); // 批量确认
        } catch (Exception e) {
            // 出错 重新确认
            channel.basicNack(deliveryTag, true, true); // 批量确认 并且重新入队
        }
    }

    private String creatContent(EmailUser emailUser) {
        StringBuilder content = new StringBuilder();
        content.append("尊敬的").append(emailUser.getUserName()).append(", 您好! <br/>")
                .append("感谢您注册成为我们博客社区的⼀员! 我们很⾼兴您决定加⼊我们的⼤家庭. <br/>")
                .append("您的注册信息如下: </br/>")
                .append("⽤⼾名:").append(emailUser.getUserName()).append("<br/>")
                .append("为了确保您的账⼾安全，请妥善保管您的登录信息. 如果使⽤过程中, 遇到任何问题, 欢迎联系我们的⽀持团队. XXXX@bite.com <br/>")
                .append("再次感谢您的加⼊，我们期待看到您的精彩内容！<br/>")
                .append("最好的祝愿<br/>")
                .append("⽐特博客团队");
        return content.toString();
    }
//    }
}
